package com.huike.game.Activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game62;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity62 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private TextView tv_game62_ss;
    private TextView tv_game62_search;
    private RequestQueue queue;
    private ProgressDialog show;
    private EditText et_game62_input;
    private ArrayList<Game62.DataBean> list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game62);
        queue = Volley.newRequestQueue(this);
        list = new ArrayList<>();
        initView();
        tv_game62_ss.setText(Html.fromHtml("<font color=''><b>搜</b></font><font color='green'><b>索</b></font><font color=''><b>历史</b></font>"));

        //搜索按钮点击
        tv_game62_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                initData();
            }
        });

    }


    public void show(){
        show = ProgressDialog.show(this, "提示", "数据加载中...");
        show.show();
    }
    public void hide(){
        show.dismiss();
    }

    public void initData(){
        final String et = et_game62_input.getText().toString();
        if(et.equals("") || et == null){
            Toast.makeText(this, "请输入查询的路线", Toast.LENGTH_SHORT).show();
            return;
        }

        show();
        final JSONObject object = new JSONObject();
        try {
            object.put("bus",et);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "traffic",object, new Response.Listener<JSONObject>() {
            @SuppressLint("ApplySharedPref")
            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();
                Log.e("---",jsonObject.toString());
                try {
                    JSONObject data = jsonObject.getJSONObject("data");
                    Log.e("---",data.getString("state"));

                    if (data.getString("state") != null || data.getString("state").equals("0")) {
                        hide();
                        Toast.makeText(GameActivity62.this, "没有查询到相关数据！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                    list.clear();
                try {
                    JSONArray data = jsonObject.getJSONArray("data");
                    Game62.DataBean bean;
                    for (int i = 0; i < data.length(); i++) {
                        bean = new Game62.DataBean();
                        JSONObject object1 = data.getJSONObject(i);
                        String platform = object1.getString("platform");
                        String name = object1.getString("name");
                        bean.setName(name);
                        bean.setPlatform(Integer.parseInt(platform));
                        bean.setClick(false);
                        list.add(bean);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                EventBus.getDefault().postSticky(list);//使用eventBus 发送数据


//                添加历史记录
                SharedPreferences game62List = getSharedPreferences("game62List", MODE_PRIVATE);
                String data = game62List.getString("data", null);

                StringBuffer buffer ;
                if(data == null){
                    buffer = new StringBuffer();
                    buffer.append(et).append("-");
                }else {
                    buffer= new StringBuffer(data);
                    buffer.append(et).append("-");
                    Log.e("---",data);
                }

                game62List.edit().putString("data",buffer.toString()).commit();

                startActivity(new Intent(GameActivity62.this, GameDescActivity62.class));





            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(GameActivity62.this, "数据加载失败！,请检查网络", Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);
    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        tv_game62_ss = (TextView) findViewById(R.id.tv_game62_ss);
        tv_game62_search = (TextView) findViewById(R.id.tv_game62_search);
        et_game62_input =  findViewById(R.id.et_game62_input);

        tv_right_text.setText("地图");
        iv_rigth_image.setVisibility(View.GONE);
        tv_content_title.setText("实时交通");
        iv_left_image.setImageResource(R.drawable.tiao);
        tv_left_text.setVisibility(View.GONE);
    }
}
